<template>
    <transition name="move" @after-leave="afterLeave">
        <div class="food" v-show="visible">
            <cube-scroll ref="scroll">
                <div class="food-content">
                    <!-- 头部图片 -->
                    <div class="image-header">
                        <img :src="food.image" alt="">
                        <div class="back" @click="back">
                            <i class="icon-arrow_lift"></i>
                        </div>
                    </div>
                    <!-- 内容 -->
                    <div class="content">
                        <h1 class="title">{{food.name}}</h1>
                        <div class="detail">
                            <span class="sell-count">月售{{food.sellCount}}份</span>
                            <span class="rating">好评率{{food.rating}}%</span>
                        </div>
                        <div class="price">
                            <span class="now">￥{{food.price}}</span>
                            <span class="on" v-show="food.oldPrice">￥{{food.oldPrice}}</span>
                        </div>
                        <div class="cart-control-wrapper">
                            <cart-control :food="food" @add="addFood"></cart-control>
                        </div>
                        <transition name="fade">
                            <div class="buy" v-show="!food.count" @click="addFirst">
                                加入购物车
                            </div>
                        </transition>
                    </div>
                    <split v-show="food.info"></split>
                    <div class="info" v-show="food.info">
                        <h1 class="title">商品信息</h1>
                        <p class="text">{{food.info}}</p>
                    </div>
                    <split></split>
                    <div class="rating">
                        <h1 class="title">商品评价</h1>
                        <rating-select :ratings="ratings" :selectType="selectType" :onlyContent="onlyContent" :desc="desc" @select="onSelect" @toggle="onToggle"></rating-select>
                        <div class="rating-wrapper">
                            <ul v-show="computedRatings || computedRatings.length">
                                <li v-for="(rating,index) in computedRatings" class="rating-item border-bottom-1px" :key="index">
                                    <div class="user">
                                        <span class="name">{{rating.username}}</span>
                                        <img :src="rating.avatar" alt="" class="avatar" width="12" height="12">
                                    </div>
                                    <div class="time">{{format(rating.rateTime)}}</div>
                                    <p class="text">
                                        <span :class="{'icon-thumb_up':rating.rateType===0,'icon-thumb_down':rating.rateType===1}"></span>{{rating.text}}
                                    </p>
                                </li>
                            </ul>
                            <div class="no-rating" v-show="!computedRatings || !computedRatings.length">暂无评价</div>
                        </div>
                    </div>
                </div>
            </cube-scroll>
        </div>
    </transition>
</template>
<script>
    import popupMixin from 'common/mixins/popup'
    import ratingMixin from 'common/mixins/rating'
    import Split from 'components/split/split'
    import CartControl from 'components/cart-control/cart-control'
    import RatingSelect from 'components/rating-select/rating-select'
    import moment from 'moment'
    const EVENT_SHOW = 'show'
    const EVENT_ADD = 'add'
    const EVENT_LEAVE = 'leave'

    export default {
        name: 'food',
        mixins: [popupMixin, ratingMixin],
        props: {
            food: {
                type: Object
            }
        },
        data() {
            return {
                desc: {
                    all: '全部',
                    positive: '推荐',
                    negative: '吐槽'
                }
            }
        },
        computed: {
            ratings() {
                return this.food.ratings
            }
        },
        created() {
            this.$on(EVENT_SHOW, () => {
                this.$nextTick(() => {
                    this.$refs.scroll.refresh()
                })
            })
        },
        methods: {
            format(time) {
                return moment(time).format('YYYY-MM-DD hh:mm')
            },
            afterLeave() {
                this.$emit(EVENT_LEAVE)
            },
            addFirst(event) {
                this.$set(this.food, 'count', 1)
                this.$emit(EVENT_ADD, event.target)
            },
            addFood(target) {
                this.$emit(EVENT_ADD, target)
            },
            back() {
                this.visible = false
            }
        },
        components: {
            Split,
            CartControl,
            RatingSelect
        }
    }
</script>
<style lang="stylus" scoped>
@import '~common/stylus/variable';
@import '~common/stylus/mixin.styl';

.food {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 48px;
    z-index: 30;
    width: 100%;
    background: $color-white;

    &.move-enter-active, &.move-leave-active {
        transition: all 0.3s linear;
    }

    &.move-enter, &.move-leave-to {
        transform: translate3d(100%, 0, 0);
    }

    .image-header {
        position: relative;
        width: 100%;
        height: 0;
        padding-top: 100%;

        img {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        .back {
            position: absolute;
            top: 10px;
            left: 0;

            .icon-arrow_lift {
                display: inline-block;
                padding: 10px;
                font-size: $fontsize-large-xx;
                color: $color-white;
            }
        }
    }

    .content {
        position: relative;
        padding: 18px;

        .title {
            line-height: 14px;
            margin-bottom: 8px;
            font-size: $fontsize-medium;
            font-weight: 700;
            color: $color-dark-grey;
        }

        .detail {
            margin-bottom: 18px;
            line-height: 10px;
            height: 10px;

            .sell-count, .rating {
                font-size: $fontsize-small-s;
                color: $color-light-grey;
            }

            .sell-count {
                margin-right: 12px;
            }
        }

        .price {
            line-height: 24px;
            font-weight: 700;

            .now {
                margin-right: 8px;
                font-size: 14px;
                color: $color-red;
            }

            .old {
                text-decoration: line-through;
                font-size: $fontsize-small-s;
                color: $color-light-grey;
            }
        }

        .cart-control-wrapper {
            position: absolute;
            right: 12px;
            bottom: 12px;
        }

        .buy {
            position: absolute;
            right: 18px;
            bottom: 18px;
            z-index: 10;
            height: 24px;
            line-height: 24px;
            padding: 0 12px;
            box-sizing: border-box;
            border-radius: 12px;
            font-size: $fontsize-small-s;
            color: $color-white;
            background: $color-blue;
            opacity: 1;

            &.fade-enter-active, &.fade-leave-active {
                transition: all 0.3s;
            }

            &.fade-enter, &.fade-leave-active {
                opacity: 0;
                z-index: -1;
            }
        }
    }

    .info {
        padding: 18px;

        .title {
            line-height: 14px;
            margin-bottom: 6px;
            font-size: $fontsize-medium;
            color: $color-dark-grey;
        }

        .text {
            line-height: 24px;
            padding: 0 8px;
            font-size: $fontsize-small;
            color: $color-grey;
        }
    }

    .rating {
        padding-top: 18px;

        .title {
            line-height: 14px;
            margin-left: 18px;
            font-size: $fontsize-medium;
            color: $color-dark-grey;
        }

        .rating-wrapper {
            padding: 0 18px;

            .rating-item {
                position: relative;
                padding: 16px 0;

                &:last-child {
                    border-none();
                }

                .user {
                    position: absolute;
                    right: 0;
                    top: 16px;
                    display: flex;
                    align-items: center;
                    line-height: 12px;

                    .name {
                        margin-right: 6px;
                        font-size: $fontsize-small-s;
                        color: $color-light-grey;
                    }

                    .avatar {
                        border-radius: 50%;
                    }
                }

                .time {
                    margin-bottom: 6px;
                    line-height: 12px;
                    font-size: $fontsize-small-s;
                    color: $color-light-grey;
                }

                .text {
                    line-height: 16px;
                    font-size: $fontsize-small;
                    color: $color-dark-grey;

                    .icon-thumb_up, .icon-thumb_down {
                        margin-right: 4px;
                        line-height: 16px;
                        font-size: $fontsize-small;
                    }

                    .icon-thumb_up {
                        color: $color-blue;
                    }

                    .icon-thumb_down {
                        color: $color-light-grey;
                    }
                }
            }

            .no-rating {
                padding: 16px 0;
                font-size: $fontsize-small;
                color: $color-light-grey;
            }
        }
    }
}
</style>